﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCT.Entities;
using DCT.Data;
using System.IO;

public partial class Admin_DanhMuc_DMLienKetWebsite : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpLienKetWebsite + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
        if (!Page.IsPostBack)
        {
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }
    protected void Bind_Data(int PageNo)
    {
        int TotalRows = 0;
        try
        {
            TList<LienKetWebsite> Tlist_LienKetWebsite = DataRepository.LienKetWebsiteProvider.GetAll();
            gridCatalog.DataSource = Tlist_LienKetWebsite;
            gridCatalog.DataBind();
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }
    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(Pager1.CurrentIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("DMLienKetWebsite.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }
    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        using (LienKetWebsite entLienKetWebsite = new LienKetWebsite())
        {
            entLienKetWebsite.Id = Convert.ToInt32(H_CatalogId.Value);
            entLienKetWebsite.TieuDe = Common.StandardString(txtTieuDe.Text);
            entLienKetWebsite.AnhDaiDien = H_AnhDaiDien.Value;
            entLienKetWebsite.DuongDan = Common.StandardString(txtDuongDan.Text);
            entLienKetWebsite.TrangThai = chkTrangThai.Checked;

            bool updateStatus = false;
            try
            {
                if (H_AnhDaiDien.Value.Length == 0)
                {
                    entLienKetWebsite.AnhDaiDien = "no_image.jpg";
                }
                else {
                    entLienKetWebsite.AnhDaiDien = Common.StandardString(H_AnhDaiDien.Value);
                }
                if (entLienKetWebsite.Id == 0)
                {
                    updateStatus = DataRepository.LienKetWebsiteProvider.Insert(entLienKetWebsite);
                }
                else
                {
                    entLienKetWebsite.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.LienKetWebsiteProvider.Update(entLienKetWebsite);
                }
                if (!updateStatus)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LIENKETWEBSITE_CAPNHAT_THATBAI, true);
                }
                else
                {
                    H_CatalogId.Value = "0";
                    txtTieuDe.Text = "";
                    txtDuongDan.Text = "";
                    H_AnhDaiDien.Value = "no_image.jpg";
                    Bind_Data(Pager1.CurrentIndex);
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.LIENKETWEBSITE_CAPNHAT_THANHCONG, true);
                }
            }
            catch (Exception)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LIENKETWEBSITE_CAPNHAT_THATBAI, true);
            }
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        H_AnhDaiDien.Value = "no_image.jpg";
        txtTieuDe.Text = "";
        txtDuongDan.Text = "";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";

            LinkButton l = (LinkButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");
        }
    }
    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Bind_Data(Pager1.CurrentIndex);
        txtTieuDe.Text = "";
        txtDuongDan.Text = "";
        H_CatalogId.Value = "0";
        H_AnhDaiDien.Value = "no_image.jpg"; 
    }
    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            try
            {
                int Id = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.LienKetWebsiteProvider.Delete(Id);
                if (!deleteStatus)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LIENKETWEBSITE_XOA_THATBAI, true);
                }
                else
                {
                    Bind_Data(Pager1.CurrentIndex);
                    H_CatalogId.Value = "0";
                    H_AnhDaiDien.Value = "no_image.jpg";
                    txtTieuDe.Text = "";
                    txtDuongDan.Text = "";
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.LIENKETWEBSITE_XOA_THANHCONG, true);
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMLienKetWebsite.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LIENKETWEBSITE_XOA_THATBAI, true);
                return;
            }
        }
        if (e.CommandName == "Select")
        {
            try
            {
                int Id = Convert.ToInt32(e.CommandArgument.ToString());
                using (LienKetWebsite entLienKetWebsite = DataRepository.LienKetWebsiteProvider.GetById(Id))
                {
                    if (entLienKetWebsite != null)
                    {
                        H_CatalogId.Value = entLienKetWebsite.Id.ToString();
                        txtTieuDe.Text = entLienKetWebsite.TieuDe;
                        H_FileName.Value = entLienKetWebsite.DuongDan;
                        txtDuongDan.Text = entLienKetWebsite.DuongDan;
                        imgViewer.ImageUrl = Common.sPathUpLienKetWebsite + entLienKetWebsite.AnhDaiDien;

                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
    }
   
   
}